//
//  ChoiceDateView.m
//  TiantuLogistics
//
//  Created by zhishi.zhaochunlin on 16/5/3.
//  Copyright © 2016年 tiantu. All rights reserved.
//

#import "ChoiceDateView.h"
#define kValue 255.0f

 #define kHeight 240.0f
@interface ChoiceDateView () 
@property (nonatomic,strong) UIView *WhiteView;
@property (nonatomic,strong) UIView *darkView;
@property (nonatomic,strong) UIView *headerView;
@property (nonatomic,strong) UIDatePicker *datePicker;


@end
@implementation ChoiceDateView

-(id)initWithFrame:(CGRect)frame{
    if (self=[super initWithFrame:frame]) {
        self.darkView=[[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
        self.darkView.backgroundColor=[UIColor colorWithRed:95/kValue green:96/kValue blue:98/kValue alpha:0.5];
        [self addSubview:self.darkView];
        
        self.WhiteView=[[UIView alloc] initWithFrame:CGRectMake(0, SCREEN_HEIGHT-kHeight , SCREEN_WIDTH, kHeight)];
        self.WhiteView.backgroundColor=[UIColor whiteColor];
        [self addSubview:self.WhiteView];
        
        CGFloat height=40.0f;
        self.headerView=[[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, height)];
        [self.WhiteView addSubview:self.headerView];
        
        UIView *lineView=[[UIView alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(self.headerView.frame)-1, SCREEN_WIDTH, 1)];
        lineView.backgroundColor=kBackGColor;
        [self.headerView addSubview:lineView];
        
        CGFloat width=45.0;
        CGFloat padding=15.0f;
        UIButton *cancelButton=[UIButton buttonWithType:UIButtonTypeCustom];
        cancelButton.frame=CGRectMake(padding, 0, width, height);
        [cancelButton setTitle:@"取消" forState:UIControlStateNormal];
        [cancelButton.titleLabel setFont:[UIFont systemFontOfSize:15.0f]];
        [cancelButton setTitleColor:kMainColor forState:UIControlStateNormal];
        [cancelButton addTarget:self action:@selector(disMiss) forControlEvents:UIControlEventTouchUpInside];
        [self.headerView addSubview:cancelButton];
        
        CGFloat width2=100.0f;
        UILabel *titleLabel=[[UILabel alloc] initWithFrame:CGRectMake(SCREEN_WIDTH/2.0f-width2/2.0f, 0, width2, height)];
        titleLabel.text=@"选择日期";
        titleLabel.font=[UIFont systemFontOfSize:15.0f];
        titleLabel.textAlignment=NSTextAlignmentCenter;
        titleLabel.textColor=kTextColorTwo;
        [self.headerView addSubview:titleLabel];
        
        UIButton *confirmButton=[UIButton buttonWithType:UIButtonTypeCustom];
        confirmButton.frame=CGRectMake(SCREEN_WIDTH-padding-width, 0, width, height);
        [confirmButton setTitle:@"确定" forState:UIControlStateNormal];
        [confirmButton.titleLabel setFont:[UIFont systemFontOfSize:15.0f]];
        [confirmButton setTitleColor:kMainColor forState:UIControlStateNormal];
        [confirmButton addTarget:self action:@selector(confirmButtonClick:) forControlEvents:UIControlEventTouchUpInside];
        [self.headerView addSubview:confirmButton];
        
        CGFloat y=CGRectGetMaxY(self.headerView.frame);
        _datePicker=[[UIDatePicker alloc] initWithFrame:CGRectMake(0, y, SCREEN_WIDTH, CGRectGetHeight(_WhiteView.frame)-y)];
        _datePicker.datePickerMode = UIDatePickerModeDateAndTime;
        [self.WhiteView addSubview:_datePicker];
        
        //设置格式日期
        NSLocale *locale = [[NSLocale alloc] initWithLocaleIdentifier:@"zh_CN"];
        _datePicker.locale = locale;
        //当前时间创建
        NSDate *localDate = [NSDate date];
        NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
        NSDateComponents *offsetComponents = [[NSDateComponents alloc] init];
        //设置时间
        [offsetComponents setYear:1];
        [offsetComponents setMonth:1];
        [offsetComponents setDay:1];
        [offsetComponents setHour:1];
        [offsetComponents setMinute:1];
        // 设置中最大时间
        NSDate *maxDate = [gregorian dateByAddingComponents:offsetComponents toDate:localDate options:0];
        _datePicker.minimumDate = localDate;
        _datePicker.maximumDate = maxDate;

        UITapGestureRecognizer *tap=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(disMiss)];
        [self addGestureRecognizer:tap];
    }
    return self;
}
-(void)confirmButtonClick:(id)sender {
    // nsdate 格式转化为nsstring
    NSDate *pickerDate = [_datePicker date];
    self.dateBlock(pickerDate);
    [self disMiss];
}

-(void)disMiss{
    [self removeFromSuperview];
}




@end
